Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-resize-detector
Advanced tools
The react-resize-detector is a React component designed to handle resize events for React elements. It provides a simple and efficient way to trigger a function or render logic when the size of an element changes. This is particularly useful in responsive designs and when elements need to adjust based on their container's dimensions.
Basic resize detection
This feature allows you to detect the size of a component and react to changes. The `useResizeDetector` hook provides `width`, `height`, and `ref` which you attach to the component you want to monitor. The component re-renders whenever the size changes, displaying the new dimensions.
import React from 'react';
import useResizeDetector from 'react-resize-detector';
const ResponsiveComponent = () => {
const { width, height, ref } = useResizeDetector();
return (
<div ref={ref}>
Size: {width} x {height}
</div>
);
};
export default ResponsiveComponent;
OnResize callback
This feature uses the `withResizeDetector` higher-order component to monitor size changes. It provides `width`, `height`, and an `onResize` callback that is triggered on every resize event. This is useful for performing actions or calculations based on the new size.
import React from 'react';
import { withResizeDetector } from 'react-resize-detector';
class MyComponent extends React.Component {
render() {
const { width, height, onResize } = this.props;
return (
<div onResize={onResize}>
Current size: {width} x {height}
</div>
);
}
}
export default withResizeDetector(MyComponent, {
handleWidth: true,
handleHeight: true,
onResize: (width, height) => console.log(`Resized to ${width} x ${height}`)
});
react-sizeme is another package that provides similar functionality to react-resize-detector. It allows components to respond to changes in size. However, react-sizeme uses a higher-order component approach primarily, which might be less convenient than hooks provided by react-resize-detector in functional components.
This package is a polyfill for the ResizeObserver API, which is used to report changes to the dimensions of an Element's content or border box. It's more of a low-level API compared to react-resize-detector, which provides React-specific abstractions and hooks for easier integration in React applications.
Nowadays browsers have started to support element resize handling natively using ResizeObservers. We use this feature (with a polyfill) to help you handle element resizes in React.
Local demo:
git clone https://github.com/maslianok/react-resize-detector.git
cd react-resize-detector/examples
npm i && npm start
npm i react-resize-detector
// OR
yarn add react-resize-detector
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import ReactResizeDetector from 'react-resize-detector';
class App extends PureComponent {
render() {
return (
<div>
...
<ReactResizeDetector handleWidth handleHeight onResize={this.onResize} />
</div>
);
}
onResize = () => {
...
}
}
render(<App />, document.getElementById('root'));
<ReactResizeDetector handleWidth handleHeight>
{(width, height) => <div>{`${width}x${height}`}</div>}
</ReactResizeDetector>
const CustomComponent = ({ width, height }) => <div>{`${width}x${height}`}</div>;
// ...
<ReactResizeDetector handleWidth handleHeight>
<CustomComponent />
</ReactResizeDetector>;
import { withResizeDetector } from 'react-resize-detector';
const CustomComponent = ({ width, height }) => <div>{`${width}x${height}`}</div>;
export default withResizeDetector(CustomComponent);
<ResizeDetector
handleWidth
handleHeight
render={({ width, height }) => (
<div>
Width:{width}, Height:{height}
</div>
)}
/>
Prop | Type | Description | Default |
---|---|---|---|
onResize | Func | Function that will be invoked with width and height arguments | n/a |
handleWidth | Bool | Trigger onResize on width change | false |
handleHeight | Bool | Trigger onResize on height change | false |
nodeType | node | Set resize-detector's node type. You can pass any valid React node: string with node's name or element. Can be useful when you need to handle table's or paragraph's resizes | div |
skipOnMount | Bool | Do not trigger onResize when a component mounts | false |
resizableElementId | String | Id of the element we want to observe. If none provided, parentElement of the component will be used | undefined |
refreshMode | String | Possible values: throttle and debounce See lodash docs for more information. undefined - means that callback will be fired as often as ResizeObserver allows | undefined |
refreshRate | Number | Only makes sense when refreshMode is set. Important! It's a numeric prop so set it accordingly, e.g. refreshRate={500} | 1000 |
refreshOptions | Object | Only makes sense when refreshMode is set. An object in shape of { leading: bool, trailing: bool }. Please refer to lodash's docs for more info | undefined |
MIT
FAQs
React resize detector
The npm package react-resize-detector receives a total of 1,104,473 weekly downloads. As such, react-resize-detector popularity was classified as popular.
We found that react-resize-detector demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.